package com.dsy.xiangcun_manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dsy.tools.common.utils.AliyunOSSUtil;
import com.dsy.tools.common.utils.QueryObj;
import com.dsy.tools.entity.TbFood;
import com.dsy.tools.entity.TbTou;
import com.dsy.xiangcun_manage.service.ITbFoodService;
import com.dsy.xiangcun_manage.service.ITbTouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dsy
 * @since 2021-03-04
 */
@Controller
@RequestMapping("/food")
public class TbFoodController {
    @Autowired
    ITbFoodService foodService;
    @Autowired
    ITbTouService stuService;
    /**
     * 跳转到美食推荐增加页面
     * @return
     */
    @RequestMapping("/addFood")
    public String addFood(){
        return "/food/food-add";
    }
    @RequestMapping("/uploadFoodImg")
    @ResponseBody
    public Map<String,Object> uploadFoodImg(@RequestParam("imgFile") List<MultipartFile> imgFiles){
        Map<String, Object> map = new HashMap<>();
        List<String> data = new ArrayList<>();
        for (MultipartFile imgFile : imgFiles) {
            String filename = imgFile.getOriginalFilename();
            System.out.println(filename);
            try {
                if (imgFile != null) {
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(filename);
                        FileOutputStream os = new FileOutputStream(newFile);
                        os.write(imgFile.getBytes());
                        os.close();
                        imgFile.transferTo(newFile);
                        // 阿里云上传到OSS
                        String uploadUrl = AliyunOSSUtil.upLoad(newFile);
                        newFile.delete();
                        data.add(uploadUrl);
                    }
                    //
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        map.put("error",0);
        map.put("data",data);
        return map;
    }
    /**
     * 添加美食推荐
     * @param food
     * @return
     */
    @RequestMapping("/saveFood")
    public String saveFood(TbFood food, HttpSession session){
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        food.setFoodUser(tbTou.getUserId());
        food.setFoodTime(LocalDateTime.now());
        foodService.saveOrUpdate(food);
        return "redirect:/food/getAllMyFood";
    }
    /**
     * 查看当前登录用户发布的
     * @param m
     * @param page
     * @param limit
     * @param queryObj
     * @return
     */
    @RequestMapping("/getAllMyFood")
    public String getAllFood(Model m, @RequestParam(defaultValue = "1") long page, @RequestParam(defaultValue = "10") long limit, QueryObj queryObj, HttpSession session){

        QueryWrapper<QueryObj> wrapper = new QueryWrapper<>();
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        queryObj.setTid(tbTou.getUserId());
        wrapper.setEntity(queryObj);

        IPage<TbFood> foodPage = foodService.getAllFood(page,limit,wrapper);
        m.addAttribute("foodPage",foodPage);
        m.addAttribute("page",page);
        m.addAttribute("queryObj",queryObj);
        return "/food/food-base";
    }
    /**
     * 根据信息Id查询
     * @param m
     * @param foodId
     * @return
     */
    @RequestMapping("/getFoodById")
    public String getFoodById(Model m,Integer foodId){
        TbFood food= foodService.getFoodById(foodId);
        m.addAttribute("food",food);
        return "/food/food-edit";
    }
    /**
     * 修改更新美食推荐
     * @param  tbFood
     * @return
     */
    @RequestMapping("/updateFood")
    public String updateFood(TbFood tbFood){
        tbFood.setFoodTime(LocalDateTime.now());
        foodService.updateById(tbFood);
        return "redirect:/food/getAllMyFood";
    }
    /**
     * 删除美食推荐信息
     * @param foodIds
     * @return
     */
    @RequestMapping("/delFoodById")
    public String delFoodById(@RequestParam("foodId") List<Integer> foodIds){
        foodService.removeByIds(foodIds);
        return "redirect:/food/getAllMyFood";
    }

}
